package com.zyf.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zyf.pojo.SmbmsProvider;
import com.zyf.service.SmbmsProviderService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张忆菲
 * @since 2020-09-09
 */
@Controller
public class SmbmsProviderController {

    @Resource
    SmbmsProviderService  service;

    @GetMapping("/provider/findAll")
    public String findAll(Model model){
        List<SmbmsProvider> list = service.list(null);
        model.addAttribute("providers",list);
        return "provider/providerList";
    }

    @PostMapping("/provider/findAllLike")
    public String findAllLike(SmbmsProvider provider,Model model){
        QueryWrapper<SmbmsProvider> wrapper = new QueryWrapper<>();
        wrapper.like("pro_name",provider.getProName());
        List<SmbmsProvider> list = service.list(wrapper);
        model.addAttribute("providers",list);
        return "provider/providerList";
    }

    @GetMapping("/provider/toAdd")
    public String toAdd(){
        return "provider/providerAdd";
    }

    @PostMapping("/provider/add")
    public String add(SmbmsProvider provider,Model model){
        boolean save = service.save(provider);
        if (save==false){
            model.addAttribute("message","添加失败");
            return "provider/providerAdd";
        }
        return "redirect:/provider/findAll";
    }

    @GetMapping("/provider/toUpdate/{proId}")
    public String toUpdate(@PathVariable("proId") Integer proId,Model model){
        SmbmsProvider byId = service.getById(proId);
        model.addAttribute("provider",byId);
        return "provider/providerUpdate";
    }

    @PostMapping("/provider/update")
    public String update(SmbmsProvider provider,Model model){
        boolean b = service.updateById(provider);
        if (b==false){
            model.addAttribute("message","修改失败");
            return "provider/providerUpdate";
        }
        SmbmsProvider smbmsProvider = service.getById(provider.getProId());
        model.addAttribute("provider",smbmsProvider);
        return "provider/providerView";
    }

    @PostMapping("/provider/delete")
    public String delete(SmbmsProvider provider,Model model){
        boolean b = service.removeById(provider.getProId());
        if (b==false){
            model.addAttribute("message","删除失败");
            return "provider/providerView";
        }
        return "redirect:/provider/findAll";
    }
}

